package com.lanqiao.companymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanqiao.companymanage.entity.Management;
import com.lanqiao.companymanage.service.OperatingMessService;
import com.lanqiao.companymanage.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 何小棍
 * 经营信息管理与添加
 */
@RestController
@RequestMapping("/operatingMess")
@Api(tags = "经营信息管理")
public class OperatingMessController {

    @Autowired
    OperatingMessService operatingMessService;

    /**
     * 分页查询
     */
    @GetMapping("/depPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数,默认第一页"),
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10")})
    @ApiOperation(value = "分页查询")
    public Result depPage(Page<Management> managementPage) {
        Page<Management> page = operatingMessService.listPage(managementPage);
        return Result.success(page);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation(value = "根据id查询")
    public Result selectById(Integer id) {
        Management operatingMess = operatingMessService.selectById(id);
        if (operatingMess == null) {
            return Result.err("找不到对应id的值");
        } else {
            return Result.success("查询成功", operatingMess);
        }
    }

    /**
     * 经营信息管理添加
     *
     * @param operatingMess
     * @return
     */
    @PostMapping("/addOperatingMess")
    @ApiOperation(value = "经营信息管理添加")
    public Result addOperatingMess(Management operatingMess) {
        int i = operatingMessService.addOperatingMess(operatingMess);
        if (i == 0) {
            return Result.err("添加失败");
        } else {
            return Result.success("添加成功", null);
        }
    }

    /**
     * 根据id删除
     */
    @GetMapping("/deletedById")
    @ApiOperation(value = "根据id删除")
    public Result deletedById(Integer id) {
        int i = operatingMessService.deleteById(id);
        if (i == 0) {
            return Result.err("删除失败");
        } else {
            return Result.success("删除成功", null);
        }
    }

    /**
     * 根据id修改
     */
    @PostMapping("/updateOperatingMess")
    @ApiOperation(value = "根据id修改")
    public Result updateOperatingMess(Management operatingMess) {
        int i = operatingMessService.updateOperatingMess(operatingMess);
        if (i == 0) {
            return Result.err("修改失败");
        } else {
            return Result.success("修改成功", null);
        }
    }

    /**
     * 查询manage的投入、收益和利润
     * @return
     */
    @RequestMapping("/showTableTwo")
    public Map<String, String> showTableTwo() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("totalOut", String.valueOf(operatingMessService.listMaps(new QueryWrapper<Management>().select("sum(touru) as totalOut")).get(0).get("totalOut")));
        hashMap.put("totalIn", String.valueOf(operatingMessService.listMaps(new QueryWrapper<Management>().select("sum(shouyi) as totalIn")).get(0).get("totalIn")));
        hashMap.put("totalLose", String.valueOf(operatingMessService.listMaps(new QueryWrapper<Management>().select("sum(lirun) as totalLose")).get(0).get("totalLose")));
        return hashMap;
    }
}
